package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductHardwareDao;
import yl.hs.bmipfull.daos.production.IProducthardwareDetailDao;
import yl.hs.bmipfull.pojo.production.ProductHardwareEntity;
import yl.hs.bmipfull.pojo.production.ProducthardwareDetailEntity;
import yl.hs.bmipfull.service.production.IProducthardwareDetailService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  服务层接口实现<br/>
 * 创建时间: 2023年07月23日 16时47分37秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProducthardwareDetailServiceImpl extends ProductionBaseService implements IProducthardwareDetailService {

    @Autowired
    IProducthardwareDetailDao dao;
    @Autowired
    IProductHardwareDao hardwareDao;

    @Override
    public List<ProducthardwareDetailEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProducthardwareDetailEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<Object> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProducthardwareDetailEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        PageResult<Object> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        List<Object> results = new ArrayList<>();
        var data = dao.queryPage(pageQuery.getQuery());
        for (ProducthardwareDetailEntity datum : data) {
            var map = datum.toMap();
            var hardware = hardwareDao.findBySN(datum.getHarewareSN());
            if(hardware!=null){
                map.put("cateName",hardware.getCateName());
                map.put("name",hardware.getName());
                map.put("space",hardware.getSpace());
                map.put("unit",hardware.getUnit());
            }
            results.add(map);
        }
        result.setData(results);
        return result;
    }


    @Override
    public ProducthardwareDetailEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveproduct_hardware_detail(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProducthardwareDetailEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProducthardwareDetailEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProducthardwareDetailEntity.class));
            var hardware = hardwareDao.findBySN(entity.getHarewareSN());
            if(hardware!=null){
                if(entity.getType()==0 || entity.getType()==3){
                    if(hardware.getQuantity()<entity.getQuantity()){
                        return -1;
                    }
                    if(entity.getType()==0){
                        writeLog(ActionType.UPDATE, entity.getHarewareSN(), "创建领用记录，库存由"+hardware.getQuantity()+"变为"+(hardware.getQuantity()-entity.getQuantity()), Tl.toJson(hardware), ProductHardwareEntity.class);
                    }else{
                        writeLog(ActionType.UPDATE, entity.getHarewareSN(), "创建破损记录，库存由"+hardware.getQuantity()+"变为"+(hardware.getQuantity()-entity.getQuantity()), Tl.toJson(hardware), ProductHardwareEntity.class);
                    }
                    hardware.setQuantity(hardware.getQuantity()-entity.getQuantity());
                }else{
                    writeLog(ActionType.UPDATE, entity.getHarewareSN(), "创建入库/退库记录，库存由"+hardware.getQuantity()+"变为"+(hardware.getQuantity()+entity.getQuantity()), Tl.toJson(hardware), ProductHardwareEntity.class);
                    hardware.setQuantity(hardware.getQuantity()+entity.getQuantity());
                }
                hardwareDao.update(hardware);
            }
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入信息", Tl.toJson(entity), ProducthardwareDetailEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新信息", Tl.toJson(entity), ProducthardwareDetailEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        var sns = sn.split(",");
        var hardware = dao.findBySN(sn);
        Integer result = dao.changeStateBySN(sns, DataState.DELETED.code);
        if (result > 0) {
            var entity = hardwareDao.findBySN(hardware.getHarewareSN());
            if(hardware!=null && entity!=null){
                if(hardware.getType()==0 || hardware.getType()==3){
                    writeLog(ActionType.UPDATE, entity.getSn(), "删除领用/破损记录，库存由"+entity.getQuantity()+"变为"+(entity.getQuantity()+hardware.getQuantity()), Tl.toJson(entity), ProductHardwareEntity.class);
                    entity.setQuantity(entity.getQuantity()+hardware.getQuantity());
                }else{
                    writeLog(ActionType.UPDATE, entity.getSn(), "删除入库/退库记录，库存由"+hardware.getQuantity()+"变为"+(entity.getQuantity()-hardware.getQuantity()), Tl.toJson(entity), ProductHardwareEntity.class);
                    entity.setQuantity(entity.getQuantity()-hardware.getQuantity());
                }
                hardwareDao.update(entity);
            }
            writeLog(ActionType.DELETE, sns, ProducthardwareDetailEntity.class);
        }
        return result;
    }
}